
use autodie;
sub test_file{
	my $filename = $_[0];
	open TEST_HANDLE, "<", $filename;
	my @test_scripts;
	while (<TEST_HANDLE>){
#		print $_;
		if (/##T([\w\W]*)\Z/){
			push @test_scripts, [1, $1];
		}elsif (/##D([\w\W]*)\Z/){
			push @test_scripts, [0, $1];
		}
	}
	\@test_scripts;
}

sub eval_scripts{
	my $verbose = $_[1]//0;
	my @ans;
	my ($true, $false, $def) = (0, 0, 0);
	foreach (@{$_[0]}){
		my ($type, $script) = (@{$_});
		my $tmp = (eval $script) ? ++$true && 0 : (($type)? (++$false && 2): ++$def && 1);
		push @ans, [$tmp, $script];
	}
	[$true, $false, $def, \@ans];
}

sub report{
	my ($stat, $ts, $ds, $fs) = ($_[0], $_[1]//0, $_[2]//0, $_[3]//1);
	my ($t, $f, $d, $ans) = @{$stat};
	print ("\nSUC: $t\tFAIL: $f\tDEF: $d\n".("-"x 18)."\n");
	foreach (@{$ans}){
		my ($type, $script) = (@{$_});
		if ($type == 0){
			print "TEST PASSED!\t$script" if ($ts!=0);
		}elsif ($type == 1){
			print "DEFINED!\t$script" if ($ds != 0);
		}else{
			print "TEST FAILED\t$script" if ($fs != 0);
		}
	}
	print (("-"x 18)."\nSUC: $t\tFAIL: $f\tDEF: $d\n");

}

require($ARGV[0]);
print "---\n\n\n";
&report(&eval_scripts(&test_file($ARGV[0])), 0, 0, 1);
